package com.xing.msm.receiver;

/**
 * @author 邢福豪
 * @date 2022/8/10 17:00
 */

import com.xing.hospital.model.vo.msm.MsmVo;
import com.xing.msm.service.MsmService;
import com.xing.rabbitmq.constant.MqConst;
import org.springframework.messaging.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.amqp.rabbit.annotation.RabbitListener;


/**
 * 消息接收器
 */
@Component
public class MsmReceiver extends Thread {

    @Autowired
    private MsmService smsService;

//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = MqConst.QUEUE_MSM_ITEM, durable = "true"),
//            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_MSM),
//            key = {MqConst.ROUTING_MSM_ITEM}
//    ))
//    public void send(MsmVo msmVo, Message message, Channel channel) {
//        System.out.println("xfh====>监听到消息，消费开始");
//        smsService.send(msmVo);
//        System.out.println("xfh====>监听到消息，消费结束");
//    }

//

    @Override
    public void run() {
        //多线程的用法，和异步线程的用法。暂时不用，后期再用

    }


    //测试用
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_MSM_ITEM, durable = "true"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_MSM),
            key = {MqConst.ROUTING_MSM_ITEM}
    ))
    public void send(Message message) {
        MsmVo msmVo = (MsmVo) message.getPayload();
//        try {
//            Thread.sleep(1000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        //mq深入了解，并且保证消息百分之百进行可达，参考阿昌爱吃黄桃中间件rabbitMQ的理解，有confirm和return 和手动ACK这块内容的
        System.out.println("xfh====>监听到消息，消费开始");
        System.out.println("msmVo消息" + msmVo);
        System.out.println("msmVo消息的手机号" + msmVo.getPhone());
        System.out.println("msmVo消息的模板代码code" + msmVo.getTemplateCode());
        System.out.println("msmVo消息的参数" + msmVo.getParam());
        System.out.println("message消息" + message);
        System.out.println("xfh====>监听到消息，消费结束");

        smsService.send(msmVo);


    }


}
